package cn.starremote.component.utils;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.*;
import java.net.MalformedURLException;

import java.util.ArrayList;

/**
 * @description:文件处理工具类
 * @Author: wangjw
 **/
@Slf4j
public class FileUtil {
    /**
     * 读取文件为string
     * @Author: wangjw
     * @param filePath
     * @return
     */
    public static String readFileReader(String filePath) {
        StringBuffer buffer = new StringBuffer();
        File file = new File(filePath);
        FileInputStream inputStream = null;
        InputStreamReader streamReader = null;
        BufferedReader reader = null;
        try{
            if(file != null){
                inputStream = new FileInputStream(file);
                streamReader = new InputStreamReader(inputStream);
                reader = new BufferedReader(streamReader);
                String line;
                while ((line = reader.readLine()) != null) {
                    buffer.append(line);
                }
                reader.close();
                streamReader.close();
                inputStream.close();
            }
        }catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            if( reader != null){
                try{
                    reader.close();
                }catch (IOException e){
                    throw new RuntimeException(e);
                }
            }
            if( streamReader != null){
                try{
                    streamReader.close();
                }catch (IOException e){
                    throw new RuntimeException(e);
                }
            }
            if( inputStream != null){
                try{
                    inputStream.close();
                }catch (IOException e){
                    throw new RuntimeException(e);
                }
            }
        }
        return buffer.toString();
    }

    /**
     * 读取文件
     * @Author: wangjw
     * @param srcFile
     * @return
     * @throws IOException
     */
    public static byte[] readFile(String srcFile) throws IOException {
        FileInputStream inputStream = null;
        ByteArrayOutputStream outputStream = null;
        try {
            byte[] buffer = new byte[1024];
            inputStream = new FileInputStream(srcFile);
            outputStream = new ByteArrayOutputStream(1024);
            int i = -1;
            while ((i =inputStream.read(buffer)) > -1){
                outputStream.write(buffer,0,i);
            }
            return outputStream.toByteArray();
        }finally {
            if(null != outputStream){
                outputStream.close();
            }
            if(null != inputStream){
                inputStream.close();
            }
        }
    }
    /**
     * 写文件到指定的目录
     * @Author: wangjw
     * @param destFile
     * @param data
     * @throws IOException
     */
    public static void write2File(String destFile, byte[] data) throws IOException {
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(destFile);
            outputStream.write(data);
        }finally {
            if(null != outputStream){
                outputStream.close();
            }
        }
    }

    /**
     * 读取XML文件为rootElement
     * @Author: wangjw
     * @param filePath
     * @return
     */
    public static Element readXMLFileReader(String filePath) throws MalformedURLException {
        SAXReader reader = new SAXReader();
        Element rootElement = null;
        try {
            Document document = reader.read(new File(filePath));
            rootElement = document.getRootElement();
        } catch (DocumentException e) {
            throw new RuntimeException(e);
        }
        return rootElement;
    }

    /**
     * 读取某个文件夹下的txt所有文件
     *
     * @param path
     * @return
     * @Author: wangjw
     */
    public static ArrayList<String> getFile(String path) {
        ArrayList<String> fileList = new ArrayList<>();
        File file = new File(path);
        File[] array = file.listFiles();

        for (int i = 0; i < array.length; i++) {
            if (array[i].isFile()) {
                if (array[i].getName().endsWith("txt")) {
                    fileList.add(array[i].getPath());
                }
            } else if (array[i].isDirectory()) {
                getFile(array[i].getPath());
            }
        }
        return fileList;
    }

    /**
     * 读取明细文件
     *
     * @param filePath
     * @return
     * @Author: wangjw
     */
    public static String[] readFileReaders(String filePath) {
        StringBuffer buffer = new StringBuffer();
        File file = new File(filePath);
        FileReader fileReader = null;
        BufferedReader reader = null;
        try {
            if (file != null) {
                fileReader = new FileReader(file);
                ;
                reader = new BufferedReader(fileReader);
                String line;
                while ((line = reader.readLine()) != null) {
                    buffer.append(line);
                    buffer.append("\r\n");
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (null != reader) {
                    reader.close();
                }
                if (null != fileReader) {
                    fileReader.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return buffer.toString().split("\r\n");
    }

    public static boolean exists(String srcFile) {
        if (StrUtil.isEmpty(srcFile)){
            return false;
        }
        return cn.hutool.core.io.FileUtil.exist(srcFile);
    }
}
